//
//  FYTabBar.swift
//  Weibo
//
//  Created by 冯毅 on 16/8/8.
//  Copyright © 2016年 冯毅. All rights reserved.
//

import UIKit

class FYTabBar: UITabBar {

    
    var composeButtonClosure:(()->())?
    
    private lazy var composeButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.addTarget(self, action: "composeButtonAction", forControlEvents: .TouchUpInside)
        button.sizeToFit()
        
        return button
        
    }()
    
    func composeButtonAction(){
        
        composeButtonClosure?()
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        addSubview(composeButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        composeButton.centerX = width * 0.5
        composeButton.centerY = height * 0.5
        
        let itemWidth = width / 5
        
        var index = 0
        
        for value in subviews {
        
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.width = itemWidth
                
                value.x = CGFloat(index) * itemWidth
                
                index++
                
                if index == 2 {
                    index++
                }
                
            }
            
        }
        
    }
    
    
    
    
    
    
    
    
}
